package com.qcloud.weapp.demo.utils;

import com.qcloud.cos.COSClient;
import com.qcloud.cos.ClientConfig;
import com.qcloud.cos.sign.Credentials;

/**
 * Created by zhonghongqiang on 2017-05-12.
 */
public class CosAuthFactory {

    private static COSClient cosClient;

    private static Credentials cred;

    private static String backet = "catanddog";

    public static String getBacket() {
        return backet;
    }

    /**
     * 使用同步锁
     * @return
     */
    synchronized public static Credentials getCredentials(){
        if (cred == null){
            long appId = 1253714476;
            String secretId = "AKIDiLn8OWRQDtIcl9eU3QbbfcN0YyCUjqmW";
            String secretKey = "lMo8LSNNH5hRAZptmU0QfiaFj18xfEcv";
            // 初始化秘钥信息
            cred = new Credentials(appId, secretId, secretKey);
        }
        return cred;
    }

    /**
     * 使用同步锁
     * @return COSClient 工厂
     */
    synchronized public static COSClient getCosClient(){
        if (cosClient == null){
            // 设置要操作的bucket
            String bucketName = "catanddog";
            // 初始化秘钥信息
            Credentials cred = getCredentials();
            // 初始化客户端配置
            ClientConfig clientConfig = new ClientConfig();
            // 设置bucket所在的区域，比如华南园区：gz； 华北园区：tj；华东园区：sh ；
            clientConfig.setRegion("gz");
            // 初始化cosClient
            cosClient = new COSClient(clientConfig, cred);
        }
        return cosClient;
    }
}
